package com.example.ylsys.plants.view;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.ylsys.plants.MainActivity;
import com.example.ylsys.plants.PermissionClass.PermissionHelper;
import com.example.ylsys.plants.PermissionClass.PermissionInterface;
import com.example.ylsys.plants.R;
import com.example.ylsys.plants.Utils.ToastUtils;
import com.example.ylsys.plants.base.BaseMvpActivity;
import com.example.ylsys.plants.base.MyApplication;
import com.example.ylsys.plants.bean.LoginParamsBean;
import com.example.ylsys.plants.bean.LoginUserBean;
import com.example.ylsys.plants.contract.WelcomingContract;
import com.example.ylsys.plants.presenter.WelcomingPresenter;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomingActivity extends BaseMvpActivity<WelcomingPresenter> implements WelcomingContract.View,PermissionInterface {
   MyApplication glovar;
   @BindView(R.id.iv_entry) ImageView mIVEntry;
   private PermissionHelper mPermissionHelper;
   LoginParamsBean userParams=new LoginParamsBean();
   @Override
   public int getPermissionsRequestCode() {
      return 10000;
   }

   @Override
   public String[] getPermissions() {
      return new String[]{
              Manifest.permission.READ_PHONE_STATE,
              Manifest.permission.WRITE_EXTERNAL_STORAGE,
              Manifest.permission.ACCESS_FINE_LOCATION,
              Manifest.permission.CAMERA,
              Manifest.permission.ACCESS_COARSE_LOCATION,
              Manifest.permission.VIBRATE,
      };
   }

   @Override
   public void requestPermissionsSuccess() {
      mPresenter = new WelcomingPresenter();
      mPresenter.attachView(this);
      mIVEntry.setImageResource(R.drawable.welcoming);
      glovar = (MyApplication) getApplication();
      Date nowDate = new Date(System.currentTimeMillis());
      if (glovar.getSessionId() == null || (nowDate.getTime() - glovar.getSessSaveTime().getTime()) > 600000) {
         String signature = null;
         userParams = mPresenter.get_userParams(this);
         if (userParams.getMd5Secret() == null) {
           goThenKill(LoginDialogActivity.class);
         }else {
            glovar.setMd5Secret(userParams.getMd5Secret());
            mPresenter.get_role(userParams);
         }

      }else {
         ToastUtils.showShort(this, "登陆成功");
         goThenKill(MainActivity.class);
      }
   }

   @Override
   public void requestPermissionsFail() {

   }

   @Override
   public int getLayoutId() {
      return R.layout.activity_welcome;
   }

   @Override
   public void initView() {
      mPermissionHelper = new PermissionHelper(WelcomingActivity.this, this);
      mPermissionHelper.requestPermissions();

   }

   @Override
   public void showLoading() {

   }

   @Override
   public void hideLoading() {

   }

   @Override
   public void onError(Throwable throwable) {
      ToastUtils.showLong(this, throwable.getMessage());
      goThenKill(LoginDialogActivity.class);
   }
//登陆成功回调
   @Override
   public void onSuccess(LoginUserBean loginUserBean) {
      glovar.setMenuJson(loginUserBean.getMenuJson());
      glovar.setPlantBodyJson(loginUserBean.getPlantBodyJson());
      glovar.setSessionId(loginUserBean.getSessionId());
      glovar.setToken(loginUserBean.getToken());
      glovar.setUserName(loginUserBean.getTrueName());
      Date getDate = new Date(System.currentTimeMillis());
      glovar.setSessSaveTime(getDate);
      ToastUtils.showShort(this, "登陆成功");
      goThenKill(MainActivity.class);
   }
}
